package com.xur.springboot.service.impl;

import com.xur.springboot.entity.ChatMessage;
import com.xur.springboot.entity.User;
import com.xur.springboot.mapper.ChatMessageMapper;
import com.xur.springboot.service.ChatService;
import com.xur.springboot.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMessageMapper chatMessageMapper;

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Override
    public ResultVO sendMessage(ChatMessage message) {

        /*// 检查发送者状态
        User sender = userMapper.selectById(message.getSenderId());
        if (sender.getStatus() == 0) {
            return new ResultVO(1006, "您的账号已被禁用", null);
        }*/
        try {
            // 设置默认值
            // 使用LocalDateTime.now()替代new Date()
            message.setCreate_time(LocalDateTime.now());
            message.setStatus(0);

            int result = chatMessageMapper.insertMessage(message);
            if (result > 0) {
                // 实时推送消息
                messagingTemplate.convertAndSendToUser(
                        message.getReceiver_id().toString(),
                        "/queue/messages",
                        message
                );
                return new ResultVO(1001, "消息发送成功", message);
            }
            return new ResultVO(1000, "消息发送失败", null);
        } catch (Exception e) {
            return new ResultVO(5000, "服务器错误: " + e.getMessage(), null);
        }
    }

    @Override
    public List<ChatMessage> getHistory(Integer sender_id, Integer receiver_id) {
        return chatMessageMapper.selectHistory(sender_id, receiver_id); // 直接返回数据，不捕获异常
    }
/*    public ResultVO getHistory(Integer sender_id, Integer receiver_id) {
        try {
            List<ChatMessage> messages = chatMessageMapper.selectHistory(sender_id, receiver_id);
            return new ResultVO(1001, "查询成功", messages);
        } catch (Exception e) {
            return new ResultVO(5000, "查询失败: " + e.getMessage(), null);
        }
    }*/





    @Override
    public ResultVO markAsRead(Integer msg_id) {
        try {
            int result = chatMessageMapper.updateMessageStatus(msg_id, 1);
            return result > 0
                    ? new ResultVO(1001, "已读状态更新成功", null)
                    : new ResultVO(1000, "消息不存在", null);
        } catch (Exception e) {
            return new ResultVO(5000, "状态更新失败: " + e.getMessage(), null);
        }
    }

    @Override
    public List<ChatMessage> selectChatBysender_id(Integer sender_id, Integer receiver_id) {
        return chatMessageMapper.selectChatMessageBysender_id(sender_id, receiver_id);
    }

    @Override
    public List<ChatMessage> selectChatHistory(Integer sender_id, Integer receiver_id) {
        return chatMessageMapper.selectChatHistory(sender_id, receiver_id);
    }
    @Override
    public List<Integer> getRelatedUserIds(Integer userId) {
        return chatMessageMapper.selectRelatedUserIds(userId);
    }

}